package com.hos.check.util;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.lang.StringUtils;

public class PagingTag extends BodyTagSupport {

	private String href;

	// 当前页
	private String cparam;
	// 每页条数
	private String sparam;
	
	private String searchOperName;
	private String searchName;
	private String searchId;
	

	private int curr;// 当前页

	private int size;// 每页条数

	private int total;// 总页数

	@Override
	public int doStartTag(){
		try{
		JspWriter out = pageContext.getOut();
		String content=((HttpServletRequest)pageContext.getRequest()).getContextPath();
        href=content+"/"+href;
		if (StringUtils.isEmpty(cparam)) {
			cparam = "currentPage";
		}
		if (StringUtils.isEmpty(sparam)) {
			sparam = "pageSize";
		}

		if (!href.endsWith("?") && !href.endsWith("&")) {
			if (href.indexOf("?") == -1) {
				href = href + "?";
			} else {
				href = href + "&";
			}
		}

		if (curr <= 0) {
			curr = 1;
		} else if (curr > total) {
			curr = total;
		}
		
//		out.append("<span>");
//		// 首页
//		if (curr == 1) {
//			out.append("首页");
////			outPage=outPage.replace("!{topPage}", "###");
//		} else {
//			//outPage=outPage.replace("!{topPage}", hrefLink(href, 1, "首页"));
//			href(out, href, 1, "首页");
//		}
//		out.append(" | ");
//		// 上一页
//		if (curr == 1) {
//			//outPage=outPage.replace("!{prePage}", "###");
//			out.append("上一页");
//		} else {
//			//outPage=outPage.replace("!{prePage}", hrefLink(href, 1, "上一页"));
//			href(out, href, curr - 1, "上一页");
//		}
//	out.append(" | ");
//		// 下一页
//		if (curr == total) {
//			out.append("下一页");
//		//	outPage=outPage.replace("!{nextPage}", "###");
//		} else {
//			//outPage=outPage.replace("!{nextPage}", hrefLink(href, 1, "下一页"));
//			href(out, href, curr + 1, "下一页");
//		}
//		//out.append(" | ");
//		// 末页
//		if (curr == total) {
//		//	outPage=outPage.replace("!{lastPage}", "###");
//			out.append("末页");
//		} else {
//		//	outPage=outPage.replace("!{lastPage}", hrefLink(href, 1, "末页"));
//			href(out, href, total, "末页");
//		}
//		//outPage=outPage.replace("!{numberPage}", "###");
//		//outPage=outPage.replace("!{numberTxt}","第"+ curr + "/" + total+"页");
//		out.append("</span>");
//		out.append("<span>第");
//		out.append(curr + "/" + total);
//		out.append("页</span>");
//		//out.append(outPage);
//		//out.write(outPage);
//		}catch (Exception e) {
//			e.printStackTrace();
//		}
		
		
		
		// <span>
		// <a href='###'>首页</a>
		// </span>
		// <span>
		// <a href='###'>下一页</a>
		// </span>
		// <span>
		// <a href='###'>上一页</a>
		// </span>
		// <span>
		// <a href='###'>第几页</a>
		// </span>
		
		
		
		String outPage = "<span><a href='!{topPage}'>首页</a></span>" +
				"<span> <a href='!{nextPage}'>下一页</a></span>" +
				"<span><a href='!{prePage}'>上一页</a></span>" +
				"<span><a href='!{lastPage}'>末页</a></span>" +
				"<span><a href='!{numberPage}'>!{numberTxt}</a></span>";

		//out.append("<span>");
		// 首页
		if (curr == 1) {
			//out.append("首页");
		//	outPage=outPage.replace("!{topPage}", "###");
			outPage=outPage.replace("!{topPage}", hrefLink(href, 1, "首页"));
		} else {
			outPage=outPage.replace("!{topPage}", hrefLink(href, 1, "首页"));
			//href(out, href, 1, "首页");
		}
		//out.append(" | ");
		// 上一页
		if (curr == 1) {
			//outPage=outPage.replace("!{prePage}", "###");
			outPage=outPage.replace("!{prePage}", hrefLink(href, curr, "上一页"));
			//out.append("上一页");
		} else {
			outPage=outPage.replace("!{prePage}", hrefLink(href, curr - 1, "上一页"));
			//href(out, href, curr - 1, "上一页");
		}
	//out.append(" | ");
		// 下一页
		if (curr == total) {
		//	out.append("下一页");
//			outPage=outPage.replace("!{nextPage}", "###");
			outPage=outPage.replace("!{nextPage}", hrefLink(href, curr, "下一页"));
		} else {
			outPage=outPage.replace("!{nextPage}", hrefLink(href, curr + 1, "下一页"));
		//	href(out, href, curr + 1, "下一页");
		}
		//out.append(" | ");
		// 末页
		if (curr == total) {
//			outPage=outPage.replace("!{lastPage}", "###");
			outPage=outPage.replace("!{lastPage}", hrefLink(href, total, "末页"));
			//out.append("末页");
		} else {
			outPage=outPage.replace("!{lastPage}", hrefLink(href, total, "末页"));
			//href(out, href, total, "末页");
		}
		outPage=outPage.replace("!{numberPage}", "###");
		outPage=outPage.replace("!{numberTxt}","第"+ curr + "/" + total+"页");
//		out.append("</span>");
//		out.append("<span>第");
//		out.append(curr + "/" + total);
//		out.append("页</span>");
		//out.append(outPage);
		out.write(outPage);
		}catch (Exception e) {
			e.printStackTrace();
		}
		
//		}catch (Exception e) {
//			e.printStackTrace();
//		}
		return SKIP_BODY;
	}
	
	private String hrefLink(String href, int curr, String title){
		StringBuffer buf=new StringBuffer(href);
		buf.append(cparam).append("=")
		.append("" + curr).append("&").append(sparam).append("=")
		.append("" + size);
		
		if(!StringUtils.isEmpty(getSearchId())){
			buf.append("&searchId=!{searchId}".replace("!{searchId}", searchId));
		}
		
		if(!StringUtils.isEmpty(getSearchName())){
			buf.append("&searchName=!{searchName}".replace("!{searchName}", searchName));
		}
		
		if(!StringUtils.isEmpty(getSearchOperName())){
			buf.append("&searchOperName=!{searchOperName}".replace("!{searchOperName}", searchOperName));
		}
		
		return buf.toString();
	}

	private void href(JspWriter out, String href, int curr, String title)
			throws IOException {
		out.append("<a href=\"").append(href).append(cparam).append("=")
				.append("" + curr).append("&").append(sparam).append("=")
				.append("" + size).append("\">").append(title).append("</a>");
	}

	public int getCurr() {
		return curr;
	}

	public void setCurr(int curr) {
		this.curr = curr;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public String getHref() {
		return href;
	}

	public void setHref(String href) {
		this.href = href;
	}

	public String getCparam() {
		return cparam;
	}

	public void setCparam(String cparam) {
		this.cparam = cparam;
	}

	public String getSparam() {
		return sparam;
	}

	public void setSparam(String sparam) {
		this.sparam = sparam;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public String getSearchOperName() {
		return searchOperName;
	}

	public void setSearchOperName(String searchOperName) {
		this.searchOperName = searchOperName;
	}

	public String getSearchName() {
		return searchName;
	}

	public void setSearchName(String searchName) {
		this.searchName = searchName;
	}

	public String getSearchId() {
		return searchId;
	}

	public void setSearchId(String searchId) {
		this.searchId = searchId;
	}
	
}
